package com.resou.reader.bookstore;

import android.content.Context;
import android.support.annotation.NonNull;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.load.DecodeFormat;
import com.bumptech.glide.request.RequestOptions;
import com.resou.reader.R;
import com.resou.reader.bookdetail.BookDetailActivity;
import com.resou.reader.data.bookstore.model.PlateNovels;
import com.resou.reader.utils.DeviceDimensionsHelper;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

import static com.resou.reader.bookstore.BookStoreMainAdapter.TYPE_FINISH;
import static com.resou.reader.bookstore.BookStoreMainAdapter.TYPE_HOT_SEARCH;

public class BookStoreHotAdapter extends RecyclerView.Adapter {

    private Context mContext;
    private List<PlateNovels> mPlateNovels;
    private int mType;
    private final int mDisplayWidth;

    public BookStoreHotAdapter(Context context, int type) {
        mContext = context;
        mType = type;
        mPlateNovels = new ArrayList<>();
        mDisplayWidth = DeviceDimensionsHelper.getDisplayWidth(mContext);
    }

    public void setData(List<PlateNovels> plateNovels) {
        mPlateNovels = plateNovels;
    }

    @NonNull
    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int viewType) {
        LayoutInflater inflater = LayoutInflater.from(mContext);
        View view;
        switch (viewType) {
            case TYPE_HOT_SEARCH:
                view = LayoutInflater.from(mContext).inflate(R.layout.recycler_item_type_hot_search_item, viewGroup, false);
                return new HotSearchHolder(view);
            case TYPE_FINISH:
                view = LayoutInflater.from(mContext).inflate(R.layout.recycler_item_type_finish_item, viewGroup, false);
                return new FinishHolder(view);
            default:
                return new DefaultHolder(LayoutInflater.from(mContext).inflate(R.layout.recycler_item_type_hot_search_item, viewGroup, false));
        }
    }


    @Override
    public void onBindViewHolder(@NonNull RecyclerView.ViewHolder viewHolder, int position) {
        final PlateNovels item = mPlateNovels.get(position);
        if (viewHolder instanceof HotSearchHolder) {
            HotSearchHolder holder = (HotSearchHolder) viewHolder;
            holder.tvAuthor.setText(item.getNovelAuthor());
            holder.tvBookName.setText(item.getNovelName());
            holder.tvBookIntro.setText(item.getNovelSummary());
            holder.tvTag1.setVisibility(View.GONE);
            holder.tvTag2.setText(item.getNovelCategoryName());
            Glide.with(mContext).setDefaultRequestOptions(new RequestOptions().format(DecodeFormat.PREFER_RGB_565)).load(item.getNovelCover()).into(holder.ivCover);
            holder.itemView.setOnClickListener(v -> BookDetailActivity.startDetailActivity(mContext, item.getNovelId()));
        }
        if (viewHolder instanceof FinishHolder) {
            FinishHolder holder = (FinishHolder) viewHolder;
            holder.tvBookName.setText(item.getNovelName());
            Glide.with(mContext).setDefaultRequestOptions(new RequestOptions().format(DecodeFormat.PREFER_RGB_565)).load(item.getNovelCover()).into(holder.imageView);
            holder.itemView.setOnClickListener(v -> BookDetailActivity.startDetailActivity(mContext, item.getNovelId()));
        }
    }
    @Override
    public void onAttachedToRecyclerView(RecyclerView recyclerView) {
        super.onAttachedToRecyclerView(recyclerView);

        RecyclerView.LayoutManager manager = recyclerView.getLayoutManager();
        if (manager instanceof GridLayoutManager) {
            GridLayoutManager gridManager = ((GridLayoutManager) manager);

            gridManager.setSpanSizeLookup(new GridLayoutManager.SpanSizeLookup() {
                @Override
                public int getSpanSize(int position) {
                    int type = getItemViewType(position);
                    switch (type) {
                        case TYPE_FINISH:
                            return 1;
                        case TYPE_HOT_SEARCH:
                            return 3;
                    }
                    return 3;
                }
            });
        }
    }
    @Override
    public int getItemCount() {
        return mPlateNovels.size();
    }

    @Override
    public int getItemViewType(int position) {
            if (position <= 2) {
                return TYPE_FINISH;
            } else {
                return TYPE_HOT_SEARCH;
            }
    }

    class HotSearchHolder extends RecyclerView.ViewHolder {
        @BindView(R.id.iv_cover)
        ImageView ivCover;
        @BindView(R.id.tv_book_name)
        TextView tvBookName;
        @BindView(R.id.tv_book_intro)
        TextView tvBookIntro;
        @BindView(R.id.iv_author_icon)
        ImageView ivAuthorIcon;
        @BindView(R.id.tv_author)
        TextView tvAuthor;
        @BindView(R.id.tv_tag1)
        TextView tvTag1;
        @BindView(R.id.tv_tag2)
        TextView tvTag2;

        public HotSearchHolder(@NonNull View itemView) {
            super(itemView);
            ButterKnife.bind(this, itemView);
        }
    }

    class FinishHolder extends RecyclerView.ViewHolder {
        @BindView(R.id.image_view)
        ImageView imageView;
        @BindView(R.id.tv_book_name)
        TextView tvBookName;

        public FinishHolder(@NonNull View itemView) {
            super(itemView);
            ButterKnife.bind(this, itemView);
        }
    }

    class DefaultHolder extends RecyclerView.ViewHolder {
        public DefaultHolder(@NonNull View itemView) {
            super(itemView);
        }
    }
}
